//
//  ESTActivitySearchViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/20.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTActivitySearchViewController.h"
#import "TYZSearchBar.h"
#import "ESTCustomerInfoModel.h"
#import "ESTActivityCustomerInfoTableViewCell.h"
#import "ESTActivityDetailViewController.h"
#import "ESTFollowUpRecordViewController.h"

@interface ESTActivitySearchViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>
/**
 *  searchBar
 */
@property(nonatomic, strong) TYZSearchBar *searchBar;
/**
 *  TableView
 */
@property(nonatomic, strong) TYZTableView *tableView;
/**
 *  数据源
 */
@property(nonatomic, strong) NSArray<ESTCustomerInfoModel *> *modelArray;
/**
 *  搜索的关键字
 */
@property(nonatomic, copy) NSString *keyWords;
@end

@implementation ESTActivitySearchViewController
#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationView];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}
#pragma mark - 设置UI
- (void)setupNavigationView {
    TYZView *topView = [[TYZView alloc] init];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(DE_NavigationHeight);
    }];
    // 右边取消按钮
    TYZButton *cancleButton = [[TYZButton alloc] init];
    [topView addSubview:cancleButton];
    [cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topView).offset(-26 * kWidth);
        make.centerY.equalTo(topView).offset(10);
        make.width.mas_equalTo(35 * kWidth);
    }];
    [cancleButton setTitle:[@"取消" S] forState:UIControlStateNormal];
    cancleButton.titleLabel.font = TYZFont(CoreInfo.fontSize * 1.2);
    [cancleButton setTitleColor:TYZBlackColor forState:UIControlStateNormal];
    [cancleButton addTarget:self action:@selector(didClickBackButton) forControlEvents:UIControlEventTouchUpInside];
    // 左边搜索栏
    NSString *placeholder = [@"请输入关键字" S];
    TYZSearchBar *searchBar = [[TYZSearchBar alloc] initWithPlaceholder:placeholder];
    searchBar.delegate = self;
    [topView addSubview:searchBar];
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15 * kWidth);
        make.right.equalTo(cancleButton.mas_left).offset(-20 * kWidth);
        make.centerY.equalTo(cancleButton);
        make.height.mas_equalTo(28);
    }];
    [searchBar becomeFirstResponder];
    self.searchBar = searchBar;
}
- (void)setupResultView {
    // 搜索结果栏
    TYZView *resultView = [self.view viewWithTag:TAG_SIXTH_ACTIVITY_SEARCHRESULTVIEW];
    if (resultView) {
        [resultView removeFromSuperview];
    }
    resultView = [[TYZView alloc] init];
    resultView.tag = TAG_SIXTH_ACTIVITY_SEARCHRESULTVIEW;
    [self.view addSubview:resultView];
    [resultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(20);
        make.top.equalTo(self.view).offset(DE_NavigationHeight);
    }];
    TYZLabel *label = [[TYZLabel alloc] init];
    [resultView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(resultView).offset(20);
        make.centerY.equalTo(resultView);
    }];
    label.font = TYZFont(CoreInfo.fontSize * 0.8);
    label.text = [NSString stringWithFormat:@"%@%lu%@", [@"找到相关结果" S], (unsigned long)self.modelArray.count, [@"个" S]];
}
- (void)setupTableView {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(DE_NavigationHeight + 20);
    }];
    [self setupTableViewHeader];
}
- (void)setupTableViewHeader {
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self acquiredData];
    }];
}
#pragma mark - actions
- (void)didClickBackButton {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)acquiredData {
    WEAKSELF
    [TYZLoadingView showMessage:nil];
    [ESTCustomerInfoModel searchCustomerListDataWithKeyWords:self.keyWords success:^(NSArray<ESTCustomerInfoModel *> *modelArray, NSInteger dataNumber) {
        [TYZLoadingView hideHUD];
        weakSelf.modelArray = modelArray;
    } failure:^(NSString *error) {
        [TYZLoadingView showError:NetworkUnusual];
    }];
}
#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.keyWords = searchBar.text;
    [searchBar resignFirstResponder];
    [self acquiredData];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CustomerInfoTableViewCell_FirstPartHeight + ActivityAchieveTableViewCell_LinePadding + ActivityAchieveTableViewCell_ContentLineHeight * 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return Value_Height_PartPadding;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TYZView *view = [[TYZView alloc] init];
    view.backgroundColor = ESTBackgroundColor;
    return view;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.modelArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ESTActivityCustomerInfoTableViewCell *cell = [ESTActivityCustomerInfoTableViewCell creatActivitySearchResultTableViewCell:tableView];
    cell.model = self.modelArray[indexPath.section];
    WEAKSELF
    [cell setDidClickActivityNameBlock:^(NSString *activityID) {
        ESTActivityDetailViewController *controller = [[ESTActivityDetailViewController alloc] init];
        controller.activity_id = activityID;
        [weakSelf.navigationController pushViewController:controller animated:YES];
    }];
    [cell setComplementDialBlock:^(NSString *duration) {
        ESTFollowUpRecordViewController *controller = [[ESTFollowUpRecordViewController alloc] init];
        controller.followUpType = FollowUpRecordType_Normal;
        controller.duration = duration;
        controller.followUpType = FollowUpRecordType_Phone;
        controller.model = weakSelf.modelArray[indexPath.section];
        [weakSelf.navigationController pushViewController:controller animated:YES];
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ESTFollowUpRecordViewController *controller = [[ESTFollowUpRecordViewController alloc] init];
    controller.followUpType = FollowUpRecordType_Normal;
    controller.model = self.modelArray[indexPath.section];
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - setter & getter
- (TYZTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[TYZTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (void)setModelArray:(NSArray<ESTCustomerInfoModel *> *)modelArray {
    _modelArray = modelArray;
    [self setupResultView];
    if (modelArray.count > 0) {
        [self setupTableView];
    } else {
        [self.tableView removeFromSuperview];
        WEAKSELF
        [self.view noDataWithClick:^{
            [weakSelf acquiredData];
        }];
    }
}
@end
